import { getStoreList } from '@mall/api/store'
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import router from "@src/router";
import mall from "@src/store/mall";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    actions: ['initLocation']
  }],
  data: {
    storeList: [],
    searchList: [],
    isShowAllList: true,
    ifSearch: false,
  },
  methods: {
    //监听子组件传过来的输入框的值
    inputValueFunc(detail) {
      if (detail.detail.length > 0) {
        const keywords = detail.detail
        let list = this.data.storeList
        let len = list.length;
        let arr = [];
        for (let i = 0; i < len; i++) {
          //如果字符串中不包含目标字符会返回-1
          let name = list[i].name;
          if (name.indexOf(keywords) >= 0) {
            arr.push(list[i]);
          }
          this.setData({
            isShowAllList: false,
            ifSearch: true
          })
        }
        this.setData({
          searchList: arr
        })
      } else {
        this.setData({
          isShowAllList: true
        })
      }
    },
    onLoad: function (options) {

    },
    onLogin() {
      this.getStoreListFunc()
      this.initLocation().then(res => {
        this.getStoreListFunc(res.latitude, res.longitude)
      }).catch(() => {
        this.getStoreListFunc()
      })
    },
    getStoreListFunc(latitude, longitude) {
      let data = {};
      if (latitude) {
        data.latitude = latitude
        data.longitude = longitude
      }
      getStoreList(data).then(res => {
        res.data.forEach((item, index) => {
          if (index === 0) {
            item.isNow = true
          } else {
            item.isNow = false
          }
        })
        this.setData({
          storeList: res.data
        })
      }).catch(err => {
      })
    },
    //跳转到店铺详情页
    jumpdetail(e) {
      let storeId = e.currentTarget.dataset.item.storeId
      router.push({ name: "mall/store-detail", params: { storeId: storeId } })
    },
  }
})
